package com.mjk.common.tools.encrypt;

import com.mjk.common.tools.logger.BizLogger;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class HMACSHA1 {

	private static final String MAC_NAME = "HmacSHA1";

	private HMACSHA1(){}

	public static String hmacSHA1Encrypt(String encryptText, String encryptKey) {
		try {
			byte[] data = encryptKey.getBytes(StandardCharsets.UTF_8);
			SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
			Mac mac = Mac.getInstance(MAC_NAME);
			mac.init(secretKey);
			byte[] text = encryptText.getBytes(StandardCharsets.UTF_8);
			byte[] finalText = mac.doFinal(text);
			return Base64.encodeBase64String(finalText);
		} catch (Exception e) {
			BizLogger.error(e);
		}
		return null;
	}
	

}
